package com.taotao.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.taotao.commen.service.ApiService;


@Service
public class IndexService {
	
	 @Autowired
	private ApiService apiService;
	 
	 @Autowired
	private PropertiesService propertiesService;
	 
	/**
	 * httpclient: 获取广告数据
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 *  * {"srcB":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg",
	 * "height":240,
	 * "alt":"",
	 * "width":670,
	 * "src":"http://image.taotao.com/images/2017/11/28/201711280711302870652.jpg",
	 * "widthB":550,
	 * "href":"http://sale.jd.com/act/e0FMkuDhJz35CNt.html?cpdad=1DLSUE",
	 * "heightB":240}
	 */
	public String queryAD1() throws ClientProtocolException, IOException {
//		String url = "http://manage.taotao.com/rest/content?categoryId=96&page=1&rows=20";
		String url = this.propertiesService.TT_MANAGE_URL+this.propertiesService.TT_MANAGE_AD1_URL;
		String jsonData = this.apiService.doGet(url);
		// 解析json
		JsonNode jsonNode = PropertiesService.MAPPER.readTree(jsonData);
		// rows 的数据集合
		ArrayNode arrayNode = (ArrayNode) jsonNode.get("rows");
		if (arrayNode == null) {
			return null;
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for(JsonNode jn : arrayNode){
			Map<String, Object> map = new HashMap<String, Object>();
			String title = jn.get("title").asText();
			String imageUrl = jn.get("url").asText();
			String imagePath = jn.get("pic").asText();
			map.put("height", 240);
			map.put("alt", title);
			map.put("srcB", imagePath);
			map.put("width", 670);
			map.put("src", imagePath);
			map.put("widthB", 550);
			map.put("href", imageUrl);
			map.put("heightB", 240);
			resultList.add(map);
		}
		// 将字符串转换为json
		String resultData = PropertiesService.MAPPER.writeValueAsString(resultList);
		
		return resultData;
	}
	/**
	 * 右上方小广告
	 * 
	 *  {
	                "width": 310,
	                "height": 70,
	                "src": "/images/5440ce68Na00d019e.jpg",
	                "href": "http://c.fa.jd.com/adclick?sid=2&cid=601&aid=3614&bid=4196&unit=35984&advid=109277&guv=&url=http://sale.jd.com/mall/FQLUNlG53wbX7m.html",
	                "alt": "",
	                "widthB": 210,
	                "heightB": 70,
	                "srcB": "http://img14.360buyimg.com/da/jfs/t334/155/1756719493/14371/e367c503/5440ce6dNd056ce39.jpg"
	            }
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public String queryAD2() throws ClientProtocolException, IOException {

		String url = this.propertiesService.TT_MANAGE_URL+this.propertiesService.TT_MANAGE_AD2_URL;
		String jsonData = this.apiService.doGet(url);
		// 解析json
		JsonNode jsonNode = PropertiesService.MAPPER.readTree(jsonData);
		// rows 的数据集合
		ArrayNode arrayNode = (ArrayNode) jsonNode.get("rows");
		if (arrayNode == null) {
			return null;
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for(JsonNode jn : arrayNode){
			Map<String, Object> map = new HashMap<String, Object>();
			String title = jn.get("title").asText();
			String imageUrl = jn.get("url").asText();
			String imagePath = jn.get("pic").asText();
			map.put("width", 310);
			map.put("height", 70);
			map.put("alt", title);
			map.put("srcB", imagePath);
			map.put("src", imagePath);
			map.put("widthB", 210);
			map.put("href", imageUrl);
			map.put("heightB", 70);
			resultList.add(map);
		}
		// 将字符串转换为json
		String resultData = PropertiesService.MAPPER.writeValueAsString(resultList);
		
		return resultData;
	}

	
	
}
